/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.pojo.dto;

import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.error.LogicException;
import org.openislands.oi.pojo.vo.CommonResponse;

public class CommonResult<E> {
    private MessageCodeEnum code;
    private String message;
    private E data;

    public CommonResult(MessageCodeEnum code, String message) {
        this.code = code;
        this.message = message;
    }

    public CommonResult(MessageCodeEnum code, String message, E data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <E> CommonResult<E> success() {
        return success("", null);
    }

    public static <E> CommonResult<E> success(String message, E data) {
        return new CommonResult<>(MessageCodeEnum.SUCCESS, message, data);
    }

    public static <E> CommonResult<E> success(E data) {
        return byMessageCode(MessageCodeEnum.SUCCESS, data);
    }

    public static <E> CommonResult<E> error(MessageCodeEnum messageCode, String message, E data) {
        return new CommonResult<>(messageCode, message, data);
    }

    public static <E> CommonResult<E> error(MessageCodeEnum messageCode, String message) {
        return new CommonResult<>(messageCode, message);
    }

    public static <E> CommonResult<E> error(MessageCodeEnum messageCode) {
        return byMessageCode(messageCode, null);
    }

    public static <E> CommonResult<E> byMessageCode(MessageCodeEnum code, E data) {
        return new CommonResult<>(code, code.getMessage(), data);
    }

    public Boolean isSuccess() {
        return this.getCode().isSuccess();
    }

    public MessageCodeEnum getCode() {
        return code;
    }

    public void setCode(MessageCodeEnum code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public E getData() {
        return data;
    }

    public void setData(E data) {
        this.data = data;
    }

    public static <T> CommonResult<T> parse(CommonResponse<T> commonResponse) throws LogicException {
        MessageCodeEnum messageCodeEnum = MessageCodeEnum.valueByCode(commonResponse.getCode());
        LogicException.nonNull(messageCodeEnum, MessageCodeEnum.SYSTEM_ERROR, "unknown response code " + commonResponse);
        return new CommonResult<>(messageCodeEnum, commonResponse.getErrorMessage(), commonResponse.getData());
    }

    public static <T, E> CommonResult<T> copy(CommonResult<E> commonResult, T data) {
        return new CommonResult<>(commonResult.getCode(), commonResult.getMessage(), data);
    }
}
